package softtech.apps.sync.android.util;

import java.util.Comparator;

import softtech.apps.model.Message;

/**
 * Comparator providing a reasonable sort order for displaying row voice
 * recorder listings to a user.
 */
public class SmsListComparator implements Comparator<Message> {
	private final boolean isAscending;

	/**
	 * Gets a comparator whose sort order places name before date.
	 * 
	 * @param ascending
	 *            whether the sort order should be ascending, vs. descending
	 *            (reversed).
	 * 
	 */
	public static SmsListComparator getNameFirst(boolean ascending) {
		return new SmsListComparator(ascending);
	}

	private SmsListComparator(boolean ascending) {
		isAscending = ascending;
	}

	@Override
	public int compare(Message lhs, Message rhs) {

		// int rawCmp = rawCompare(lhs, rhs);
		int res = String.CASE_INSENSITIVE_ORDER.compare(lhs.get_title(),
				rhs.get_title());
		if (res == 0) {
			res = lhs.get_title().compareTo(rhs.get_title());
		}
		return isAscending ? res : -res;
	}

	int rawCompare(Message lhs, Message rhs) {

		return lhs.get_title().compareTo(rhs.get_title());
	}

}